package Servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //设置编码
        request.setCharacterEncoding("utf-8");
        //从登陆页面拿过来username和password
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Map<String, String[]> parameterMap = request.getParameterMap();
        //创建对象，进行赋值
       User user=new User();
       user.setUsername(username);
       user.setPassword(password);
       UserService userService= new UserServiceImpl();
       User login=userService.login(user);
        if (login==null) {
            //跳入失败的servlet  FaileServlet
        request.getRequestDispatcher("FailedServlet").forward(request,response);
        }else{
            request.setAttribute("user",login);
            //跳入成功的servlet  SuccessServlet
            request.getRequestDispatcher("SuccessServlet").forward(request,response);
        }

    }
}
